﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PurchaseAgreement.aspx.cs" Inherits="Page_采购_PurchaseAgreement" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="../../Com/easyUi/themes/icon.css" rel="stylesheet" />
    <link href="../../Com/easyUi/themes/default/easyui.css" rel="stylesheet" />
    <link href="../../Com/formValidator/css/template.css" rel="stylesheet" />
    <link href="../../Com/formValidator/css/validationEngine.jquery.css" rel="stylesheet" />


    <script src="../../JS/jquery-1.4.2.min.js"></script>
    <script src="../../Com/easyUi/jquery.easyui.min.js"></script>
    <script src="../../JS/jquery.form.js"></script>
    <script src="../../Com/formValidator/js/jquery.validationEngine.js"></script>
    <script src="../../Com/formValidator/js/languages/jquery.validationEngine-zh_CN.js"></script>

    <script type="text/javascript">
        var optionOrderDetials = {};
        var optionIssueOrderDetials = {};
        $().ready(function () {

            //订单详细列表
            optionOrderDetials = {
                iconCls: 'icon-save',
                idField: 'IDPlus',
                nowrap: false,
                fit: true,
                striped: true,
                url: '',
                loadMsg: '数据装载中......',
                sortName: 'AddTime',
                sortOrder: 'asc',
                queryParams: {},
                singleSelect: true,
                columns: [[
                    { title: '编号', field: 'IDPlus', sortable: true, align: 'center', width: 120, hidden: true },
                    { title: '商品编号', field: 'ProductSku', sortable: true, align: 'center', width: 110 },
                    { title: '商品名称', field: 'ProductName', sortable: true, align: 'center', width: 140 },
                    { title: '商品规格', field: 'ProductDesc', align: 'center', width: 80 },
                    { title: '商品数量', field: 'ProductCount', sortable: true, align: 'center', width: 80 },
                    { title: '商品单价', field: 'ProductPrice', sortable: true, align: 'center', width: 80 },
                    { title: '添加日期', field: 'AddTime', sortable: true, align: 'center', width: 140 }
                ]],
                rownumbers: true,
                onSortColumn: function (sort, order) {
                    var queryParams = $('#dataListOrder').datagrid('options').queryParams;
                    queryParams.SortName = sort;
                    queryParams.SortOrder = order;
                }
            };

            //补发订单详细列表
            optionIssueOrderDetials = {
                iconCls: 'icon-save',
                idField: 'IDPlus',
                nowrap: false,
                fit: true,
                striped: true,
                url: '',
                loadMsg: '数据装载中......',
                sortName: 'AddTime',
                sortOrder: 'asc',
                queryParams: {},
                singleSelect: true,
                columns: [[
                    { title: '编号', field: 'IDPlus', sortable: true, align: 'center', width: 120, hidden: true },
                    { title: '商品编号', field: 'ProductSku', sortable: true, align: 'center', width: 110 },
                    { title: '商品名称', field: 'ProductName', sortable: true, align: 'center', width: 140 },
                    { title: '商品规格', field: 'Spec', align: 'center', width: 80 },
                    { title: '商品数量', field: 'ProductCount', sortable: true, align: 'center', width: 80 },
                    { title: '商品单价', field: 'ProductPrice', sortable: true, align: 'center', width: 80 },
                    { title: '添加日期', field: 'AddTime', sortable: true, align: 'center', width: 140 }
                ]],
                rownumbers: true,
                onSortColumn: function (sort, order) {
                    var queryParams = $('#issuedataListOrder').datagrid('options').queryParams;
                    queryParams.SortName = sort;
                    queryParams.SortOrder = order;
                }
            };

            //商品详情
            var detialOption = {
                iconCls: 'icon-save',
                idField: 'IDPlus',
                nowrap: false,
                fit: true,
                striped: true,
                useRp: true,
                url: '/Handler/OrderHandler.ashx?action=GetOldOrderDetial',
                loadMsg: '数据装载中......',
                sortOrder: 'asc',
                queryParams: {},
                singleSelect: true,
                columns: [[
                    { title: '编号', field: 'OrderNo', sortable: true, align: 'center', width: 110 },
                    { title: 'Id', field: 'MdIDPlus', sortable: true, align: 'center', width: 100, hidden: true },
                    { title: 'Spec', field: 'spec', sortable: true, align: 'center', width: 100, hidden: true },
                    { title: 'SpecId', field: 'SpecIDPlus', sortable: true, align: 'center', width: 100, hidden: true },
                    { title: '编码', field: 'ProductSku', sortable: true, align: 'center', width: 140 },
                    { title: '商品名称', field: 'ProductName', sortable: true, align: 'center', width: 240 },
                    { title: '单价', field: 'ProductPrice', sortable: true, align: 'center', width: 80 },
                    { title: '数量', field: 'ProductCount', sortable: true, align: 'center', width: 80 },
                    { title: '库存数', field: 'Stock', sortable: true, align: 'center', width: 100 },
                    { title: '待发货库存', field: 'WaitSendStock', sortable: true, align: 'center', width: 100 },
                    {
                        title: "操作",
                        field: "opt",
                        width: 120,
                        align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            var strOption = "";
                            strOption += '<a href="#" class="easyui-linkbutton" style ="color:blue;" onclick="getSelected(' + rowIndex + ')">添加至购物车</a>';
                            return strOption;
                        }
                    }
                ]],
                onSortColumn: function (sort, order) {
                    var queryParams = $('#dataList').datagrid('options').queryParams;
                    queryParams.SortName = sort;
                    queryParams.SortOrder = order;
                },
                onLoadSuccess: function (row) {//当表格成功加载时执行               
                    var rowData = row.rows;

                    $("#txtReceiveName").val(rowData[0].ReceiveName);
                    $("#txtPhone").val(rowData[0].ReceivePhone);
                    $("#txtAdress1").val(rowData[0].ReceiveProvince);
                    $("#txtAdress2").val(rowData[0].ReceiveCity);
                    $("#txtAdress3").val(rowData[0].ReceiveArea);
                    $("#txtAdress").val(rowData[0].ReceiveAddress);
                    $("#txtRemarks").val(rowData[0].Remark);
                }
            };
            $("#DetialList").datagrid(detialOption);

            //补发列表
            var option = {
                iconCls: 'icon-save',
                idField: 'IDPlus',
                nowrap: false,
                fit: true,
                striped: true,
                useRp: true,
                pageList: [1, 10, 15, 20],
                pageSize: 20,
                url: '/Handler/OrderHandler.ashx?action=GetIssueOrderTable',
                loadMsg: '数据装载中......',
                sortName: 'AddTime',
                sortOrder: 'Desc',
                queryParams: {},
                singleSelect: true,
                columns: [[
                    {
                        title: "操作",
                        field: "opt",
                        width: 180,
                        align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            var strOption = "";
                            strOption += '<a href="#" style="text-decoration:none;color:blue" onclick="LookDetails(\'' + rowData.OrderNo
                                + '\',\'' + rowData.OriginalOrderNo + '\');">订单详情</a>&nbsp&nbsp';
                            return strOption;
                        }
                    },
                    { title: '订单号', field: 'OrderNo', sortable: true, align: 'center', width: 110 },
                    { title: '下单日期', field: 'AddTime', sortable: true, align: 'center', width: 140 },
                    { title: '订单金额', field: 'OrderMoney', sortable: true, align: 'center', width: 80 },
                    { title: '订单状态', field: 'OrderStateval', sortable: true, align: 'center', width: 80 },
                    { title: '订单类型', field: 'OrderTypeval', sortable: true, align: 'center', width: 80 },
                    { title: '收货人姓名', field: 'ReceiveName', sortable: true, align: 'center', width: 100 },
                    { title: '收货人电话', field: 'ReceivePhone', sortable: true, align: 'center', width: 100 },
                    { title: '收货人地址', field: 'ReceiveAddress', sortable: true, align: 'center', width: 200 },
                    { title: '店主姓名', field: 'Name', sortable: true, align: 'center', width: 100 },
                    { title: '店主电话', field: 'Phone', sortable: true, align: 'center', width: 100 },
                    { title: '店主等级', field: 'RoleName', sortable: true, align: 'center', width: 100 },
                    { title: '操作人', field: 'EmployeeName', sortable: true, align: 'center', width: 100 }

                ]],
                pagination: true,
                rownumbers: true,
                toolbar: "#divToolBars",
                onSortColumn: function (sort, order) {
                    var queryParams = $('#dataList').datagrid('options').queryParams;
                    queryParams.SortName = sort;
                    queryParams.SortOrder = order;
                }
            };
            option.queryParams = { RP: option.pageSize };
            $("#dataList").datagrid(option);
            //分页实现
            var p = $('#dataList').datagrid('getPager');
            if (p) {
                $(p).pagination({
                    displayMsg: '当前显示从{from}到{to}共{total}记录',
                    beforePageText: "第",
                    afterPageText: "共 {pages}",
                    onBeforeRefresh: function (pageNumber, pageSize) {
                        $(this).pagination('loading');
                    },
                    onChangePageSize: function () {
                        var queryParams = $('#dataList').datagrid('options').queryParams;
                        queryParams.RP = $(p).pagination('options').pageSize;
                    },
                    onRefresh: function () {
                    },
                    onSelectPage: function (pageNumber, pageSize) {
                        var queryParams = $('#dataList').datagrid('options').queryParams;
                        queryParams.Page = pageNumber;
                        $("#dataList").datagrid('reload');
                    }
                });
            }



            //查看订单信息
            $("#dialogLookOrder").dialog({
                title: "查看订单",
                width: 800,
                height: 580,
                buttons: [
                    {
                        text: '关闭',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $("#dialogLookOrder").dialog("close");
                        }
                    }]
            });

            //添加产品标签
            $("#dialogAddTag").dialog({
                title: "补发订单",
                width: 1000,
                height: 620,
                buttons: [
                {
                    text: '关闭',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        $("#dialogAddTag").dialog("close");

                    }
                }],
                onClose: function () {
                    $("#txtKeyWords").val("");
                    $("#txtReceiveName").val("");
                    $("#txtPhone").val("");
                    $("#txtAdress1").val("");
                    $("#txtAdress2").val("");
                    $("#txtAdress3").val("");
                    $("#txtAdress").val("");
                    $("#txtRemarks").val("");
                    $('#cartOrderDetial').datagrid('loadData', { total: 0, rows: [] });
                    $(".orderMoney").text(0);
                    Query();

                }
            });


        });



        //表单提交前
        function showRequest(formData, jqForm, options) {
            return true;
        }
        //表单提交出错
        function showError(data) {
            alert('提交出错！');
        }


        //订单详细
        function LookDetails(orderNo, originalOrderNo) {

            optionOrderDetials.url = "/Handler/OrderHandler.ashx?action=GetAgentsOrderDetailTable&OrderNo=" + originalOrderNo;
            optionIssueOrderDetials.url = "/Handler/OrderHandler.ashx?action=GetAgentsOrderDetailTable&OrderNo=" + orderNo;
            $("#dataListOrder").datagrid(optionOrderDetials);
            $("#issuedataListOrder").datagrid(optionIssueOrderDetials);
            $.post('/Handler/OrderHandler.ashx?action=GetOrderDetial', { OrderNo: originalOrderNo }, function (data) {
                var list = eval("(" + data + ")");
                $("#sOrderNo").html(originalOrderNo);
                $("#sOrderTime").html(list.rows[0].AddTime);
                $("#sOrderPrice").html(list.rows[0].OrderMoney);
                $("#sPostageMoney").html(list.rows[0].PostageMoney);
                $("#sMemberName").html(list.rows[0].PostageMoney);
                $("#sMemberPhone").html(list.rows[0].ReceivePhone);
            });
            //#endregion
            $("#dialogLookOrder").dialog("open");


        }

        //查询补发列表
        function QueryIssue() {
            var search = "";
            $('#divToolBars').find(":text,:selected,select,textarea,:hidden,:checkbox,:password").each(function () {
                search = search + this.id + "&" + this.value + "^";
            });
            var queryParams = $('#dataList').datagrid('options').queryParams;
            $('#dataList').datagrid('options').page = 1;
            queryParams.Page = 1;
            queryParams.Query = search;
            var gp = $('#dataList').datagrid('getPager');
            if (gp) {
                gp.pagination('options').pageNumber = 1;
                gp.pagination('loading');
            }
            $("#dataList").datagrid('reload'); //刷新
        }

        //查询原订单信息
        function Query() {
            var search = "";
            $('#divToolBar').find(":text,:selected,select,textarea,:hidden,:checkbox,:password").each(function () {
                search = search + this.id + "&" + this.value + "^";
            });
            var queryParams = $('#DetialList').datagrid('options').queryParams;
            $('#DetialList').datagrid('options').page = 1;
            queryParams.Page = 1;
            queryParams.Query = search;
            $("#DetialList").datagrid('reload'); //刷新
        }

        //添加至购物车
        function getSelected(rowIndex) {
            var totalprice = 0;
            var flag = false;
            var detialList = $('#DetialList').datagrid('getData');
            //var row = $('#DetialList').datagrid('getSelected');
            var cartrow = $('#cartOrderDetial').datagrid('getData');
            if (detialList) {
                var row = detialList.rows[rowIndex];
                if (cartrow.rows.length > 0) {
                    for (var i = 0; i < cartrow.rows.length; i++) {
                        var rows = cartrow.rows[i];
                        totalprice += rows.Price;
                        if (rows.MdIDPlus == row.MdIDPlus) {
                            flag = true;
                        }
                    }
                    if (flag != true) {
                        cartrow.rows.push({
                            OrderNo: row.OrderNo,
                            ProductSku: row.ProductSku,
                            ProductName: row.ProductName,
                            ProductPrice: row.ProductPrice,
                            ProductCount: '<input  type="number" class="productCount' + row.MdIDPlus + '"  onclick="adddProductCount(' + row.MdIDPlus + ')" style="text-align:center;width:70px;" min="1" value="' + row.ProductCount + '"style="width: 0px;"/>',
                            Price: row.ProductCount * row.ProductPrice,
                            ProductCount1: '<a href="#" class="easyui-linkbutton" style ="color:blue;" onclick="DelOrder(' + row.MdIDPlus + ')">删除</a>',
                            MdIDPlus: row.MdIDPlus,
                            Spec: row.spec,
                            SpecId: row.SpecIDPlus
                        });
                        totalprice += row.ProductCount * row.ProductPrice;

                    }

                } else {
                    cartrow.rows.push({
                        OrderNo: row.OrderNo,
                        ProductSku: row.ProductSku,
                        ProductName: row.ProductName,
                        ProductPrice: row.ProductPrice,
                        ProductCount: '<input  type="number" class="productCount' + row.MdIDPlus + '"  onclick="adddProductCount(' + row.MdIDPlus + ')" style="text-align:center;width:70px;" min="1" value="' + row.ProductCount + '"style="width: 0px;"/>',
                        Price: row.ProductCount * row.ProductPrice,
                        ProductCount1: '<a href="#" class="easyui-linkbutton" style ="color:blue;" onclick="DelOrder(' + row.MdIDPlus + ')">删除</a>',
                        MdIDPlus: row.MdIDPlus,
                        Spec: row.spec,
                        SpecId: row.SpecIDPlus
                    });
                    totalprice += row.ProductPrice * row.ProductCount;
                }

                $('#cartOrderDetial').datagrid('loadData', cartrow);
                $(".orderMoney").text(totalprice);

            }
        }

        //删除
        function DelOrder(MdIDPlus) {
            var cartrow = $('#cartOrderDetial').datagrid('getData');
            if (cartrow.rows.length > 0) {
                var price = parseInt($(".orderMoney").text());
                MdIDPlus = parseInt(MdIDPlus);
                for (var i = 0; i < cartrow.rows.length; i++) {
                    var mdidplus = parseInt(cartrow.rows[i].MdIDPlus);
                    if (mdidplus == MdIDPlus) {
                        price -= parseInt(cartrow.rows[i].Price);
                        cartrow.rows.splice(i, 1);
                    }
                }
                $(".orderMoney").text(price);
                $('#cartOrderDetial').datagrid('loadData', cartrow);
            }

        }

        //增加产品数量
        function adddProductCount(MdIDPlus) {
            var productCount = $(".productCount" + MdIDPlus).val();
            var cartrow = $('#cartOrderDetial').datagrid('getData');
            var price = 0;
            if (cartrow.rows.length > 0) {
                MdIDPlus = parseInt(MdIDPlus);
                for (var i = 0; i < cartrow.rows.length; i++) {
                    var mdidplus = parseInt(cartrow.rows[i].MdIDPlus);
                    if (mdidplus == MdIDPlus) {
                        cartrow.rows[i].Price = cartrow.rows[i].ProductPrice * productCount;
                        cartrow.rows[i].ProductCount = '<input  type="number" class="productCount' + MdIDPlus + '"  onclick="adddProductCount(' + MdIDPlus + ')" style="text-align:center;width:70px;" min="1" value="' + productCount + '"style="width: 0px;"/>';
                    }
                    price += parseInt(cartrow.rows[i].Price);

                }
                $(".orderMoney").text(price);
                $('#cartOrderDetial').datagrid('loadData', cartrow);
            }
        }

        //下单
        function xiadan() {
            var cartrow = $('#cartOrderDetial').datagrid('getData');
            if (cartrow.rows.length <= 0) {
                alert("购物车为空");
                return;
            }
            var totalprice = $(".orderMoney").text();
            if (cartrow.rows.length > 0) {
                var orderdetial = JSON.stringify(cartrow.rows);
                $.post('/Handler/OrderHandler.ashx?action=PlaceOrder', { Orderdetial: orderdetial, ToTalPrice: totalprice }, function (data) {
                    var result = GetResult(data);
                    alert(result.Message);
                    $("#dialogAddTag").dialog("close");
                    $("#dataList").datagrid('reload');
                });
            }


        }

        //补发订单
        function addOrder() {
            $("#dialogAddTag").dialog("open");
        }


    </script>
</head>
<body class="easyui-layout" style="height: 95%; overflow: hidden; border: none;">
    <div region="center" border="false" style="margin-left: 5px; margin-top: 3px">
        <table id="dataList">
        </table>
    </div>

    <div id="divToolBars" style="padding: 5px; height: auto">
        <div>
            关键字：
             <input id="txtKeyWords" name="txtKeyWords" type="text" class="txt01" />
            &nbsp&nbsp
             <a id="btnSearch" href="javascript:void(0);" iconcls="icon-search" class="easyui-linkbutton" onclick="QueryIssue()">查询</a>
            <a id="btnAdd" href="javascript:void(0);" iconcls="icon-add" class="easyui-linkbutton" onclick="addOrder()">采购协议</a>

        </div>
    </div>

    <div id="dialogLookOrder" class="easyui_dialog" style="padding: 10px 10px;" closed="true">
        <div class="ftitle">订单信息</div>
        <table id="tbOrderInfo" cellpadding="3" cellspacing="1" style="width: 100%; margin: 0px auto; line-height: 26px;">
            <tr>
                <td class="td-first">订单编号：
                </td>
                <td class="td-second">
                    <span id="sOrderNo"></span>
                </td>
                <td class="td-first">下单日期：
                </td>
                <td class="td-second">
                    <span id="sOrderTime"></span>
                </td>
            </tr>
            <tr>
                <td class="td-first">产品金额：
                </td>
                <td class="td-second">
                    <span id="sOrderPrice"></span>
                </td>
            </tr>
            <tr>
                <td class="td-first">收货人电话：
                </td>
                <td class="td-second">
                    <span id="sMemberPhone"></span>
                </td>
            </tr>
        </table>
        <div class="ftitle"></div>
        <div style="padding-left: 59px; margin-bottom: 8px;">原订单商品详细：</div>
        <div class="easyui-layout" style="height: 200px; width: 700px; overflow: hidden; border: 0.2px solid #95b8e7; margin: 0px auto;">
            <div region="center" border="false" style="">
                <table id="dataListOrder">
                </table>
            </div>
        </div>
        <div class="ftitle"></div>
        <div style="padding-left: 59px; margin-bottom: 8px;">补发订单商品详细：</div>
        <div class="easyui-layout" style="height: 200px; width: 700px; overflow: hidden; border: 0.2px solid #95b8e7; margin: 0px auto;">
            <div region="center" border="false" style="">
                <table id="issuedataListOrder">
                </table>
            </div>
        </div>
    <%--</div>--%>

    <div id="dialogAddTag" class="easyui_dialog" style="padding: 10px 10px;" closed="true">
        <div class="easyui-layout" style="width: 965px; height: 700px;">
            <div data-options="region:'north',title:'信息',collapsible:false" style="height: 250px">
                <div id="divToolBar" style="padding: 5px; height: auto">
                    <div style="margin-bottom: 5px; margin-left: 10px;">
                        原订单编号：<input id="txtKeyWords" name="txtKeyWords" type="text" class="txt01" />
                        &nbsp;&nbsp;
                        <a id="btnSearch" href="javascript:void(0);" iconcls="icon-search" class="easyui-linkbutton" onclick="Query()">查询</a>

                    </div>
                </div>
                <div>
                    <table style="margin-left: 10px;">
                        <tr style="text-align: left">
                            <td style="text-align: left">收货人：</td>
                            <td style="text-align: left">
                                <input id="txtReceiveName" name="txtReceiveName" type="text" class="txt01" />
                            </td>
                            <td style="text-align: left">手机号：</td>
                            <td style="text-align: left">
                                <input id="txtPhone" name="txtPhone" type="text" class="txt01" />
                            </td>
                        </tr>
                        <tr>
                            <td style="text-align: left">配送地址：</td>
                            <td>
                                <input id="txtAdress1" name="txtAdress" type="text" class="txt01" style="width: 80px" />&nbsp;<input id="txtAdress2" name="txtAdress" type="text" class="txt01" style="width: 80px" />&nbsp;<input id="txtAdress3" name="txtAdress" type="text" class="txt01" style="width: 80px" /></td>
                        </tr>
                        <tr>
                            <td style="text-align: left">详细地址：</td>
                            <td style="text-align: left">
                                <input id="txtAdress" name="txtAdress" type="text" class="txt01" style="width: 300px" /></td>
                        </tr>
                        <tr>
                            <td style="text-align: left">备注信息：</td>
                            <td>
                                <textarea id="txtRemarks" name="txtRemarks" style="width: 350px; height: 50px"></textarea></td>
                        </tr>
                    </table>

                </div>

            </div>
            <div data-options="region:'south',title:'商品信息',split:true,collapsible:false" style="height: 200px;">
                <table id="DetialList">
                </table>
            </div>
            <div data-options="region:'east',title:'订单金额',split:true,collapsible:false" style="width: 202px;">
                <div style="margin-left: 10px; margin-top: 40px;">
                    <p style="font-size: 20px; margin-left: 20px;">订单总金额：</p>
                    <p style="color: red; font-size: 25px; margin-left: 100px;">￥<span class="orderMoney" style="font-size: 25px;"></span></p>
                    <a style="margin-left: 20px;" id="" href="javascript:void(0);" iconcls="" class="easyui-linkbutton" onclick="xiadan()">下单</a>
                </div>

            </div>
            <div data-options="region:'center',title:'购物车',collapsible:false">
                <table id="cartOrderDetial" class="easyui-datagrid" style="width: 760px; height: 200px;">
                    <thead>
                        <tr>
                            <th title="编号" field="OrderNo" width="110" align="center">编号</th>
                            <th title="编码" field="ProductSku" width="140" align="center">编码</th>
                            <th title="商品名称" field="ProductName" width="140" align="center">商品名称</th>
                            <th title="单价" field="ProductPrice" width="90" align="center">单价</th>
                            <th title="数量" field="ProductCount" width="90" align="center">数量</th>
                            <th title="小计" field="Price" width="90" align="center">小计</th>
                            <th width="90" field="ProductCount1" align="center">操作</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>


</body>
</html>
